import request from "../utils/request";

// 登录
export function login(data) {
    return request({
        method: 'POST',
        data:data,
        url: 'index.php/api/Pub/messagelogin'
    })
}

// 员工列表
export function getEmployeeList(params) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestParams = {
        ...params,
        token: token
    };
    
    return request({
        method: 'GET',
        params: requestParams,
        url: 'index.php/api/newfile.User/userList'
    })
}

// 新增员工
export function addEmployee(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestData = {
        ...data,
        token: token
    };
    
    return request({
        method: 'POST',
        data: requestData,
        url: 'index.php/api/newfile.User/userAdd'
    })
}

// 编辑员工
export function editEmployee(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestData = {
        ...data,
        token: token
    };
    
    return request({
        method: 'POST',
        data: requestData,
        url: 'index.php/api/newfile.User/userEdit'
    })
}

// 删除员工
export function deleteEmployee(userId) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestData = {
        UserId: userId,
        token: token
    };
    
    return request({
        method: 'POST',
        data: requestData,
        url: 'index.php/api/newfile.User/userDel'
    })
}

// 获取订单列表
export function getOrderList(params) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestParams = {
        ...params,
        token: token
    };
    
    return request({
        method: 'GET',
        params: requestParams,
        url: 'index.php/api/newfile.Orders/orderList'
    })
}

// 修改订单状态
export function changeOrderStatus(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestData = {
        ...data,
        token: token
    };
    
    return request({
        method: 'POST',
        data: requestData,
        url: 'index.php/api/newfile.Orders/orderStatusChange'
    })
}
//无分页工厂列表
export function getFactoryListMore() {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestParams = {
        token: token
    };
    
    return request({
        method: 'GET',
        params: requestParams,
        url: 'index.php/api/newfile.Orders/factoryList'
    })
}

// 获取员工详情
export function getEmployeeDetail(userId) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'GET',
        params: { token: token, userId: userId },
        url: 'index.php/api/newfile.User/userDetail'
    })
}




//订单管理

//客户管理

// 获取客户列表
export function getCustomerList(params) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestParams = {
        ...params,
        token: token
    };
    
    return request({
        method: 'GET',
        params: requestParams,
        url: 'index.php/api/newfile.Customer/customerList'
    })
}

// 获取客户详情
export function getCustomerDetail(customerId) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'GET',
        params: { token: token, CustomerId: customerId },
        url: 'index.php/api/newfile.Customer/customerInfo'
    })
}

// 根据企业名称搜索客户
export function searchCustomerByCompany(companyName) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'GET',
        params: { token: token, companyName: companyName },
        url: 'index.php/api/newfile.Customer/customerDetail'
    })
}

// 新增客户
export function addCustomer(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    const requestData = {
        ...data,
        token: token
    };
    
    return request({
        method: 'POST',
        data: requestData,
        url: 'index.php/api/newfile.Customer/customerAdd'
    })
}

// 获取产品列表（生成订单使用）
export function getProductList() {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'GET',
        params: { token: token },
        url: 'index.php/api/newfile.Orders/productList'
    })
}

// 获取设备列表（生成订单使用）
export function getDeviceListMore() {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'GET',
        params: { token: token },
        url: 'index.php/api/newfile.Orders/deviceListMore'
    })
}

// 生成订单
export function generateOrder(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { ...data, token: token },
        url: 'index.php/api/newfile.Orders/productOrder'
    })
}

// 根据合同ID获取订单列表
export function getOrdersByContractId(contractId) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'GET',
        params: { token: token, contract_id: contractId },
        url: 'index.php/api/newfile.Orders/getOrdersByContractId'
    })
}

// 删除订单
export function deleteOrder(orderId) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { token: token, id: orderId },
        url: 'index.php/api/newfile.Orders/orderDel'
    })
}

// 工厂管理

// 获取工厂列表
export function getFactoryList(params) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestParams = {
        ...params,
        token: token
    };
    
    return request({
        method: 'GET',
        params: requestParams,
        url: 'index.php/api/newfile.Factory/factoryList'
    })
}

// 添加工厂
export function addFactory(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { ...data, token: token },
        url: 'index.php/api/newfile.Factory/factoryAdd'
    })
}

// 编辑工厂
export function editFactory(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { ...data, token: token },
        url: 'index.php/api/newfile.Factory/factoryEdit'
    })
}

// 删除工厂
export function deleteFactory(factoryId) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { token: token, id: factoryId },
        url: 'index.php/api/newfile.Factory/factoryDel'
    })
}

//获取单个工厂的管理员列表
export function factoryAdminList(factoryId) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { token: token, factoryId: factoryId },
        url: 'index.php/api/newfile.Factory/factoryAdminList'
    })
}
//给工厂添加管理员
export function factoryAdminAdd(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { ...data, token: token },
        url: 'index.php/api/newfile.Factory/factoryAdminAdd'
    })
}

//删除工厂管理员
export function factoryAdminDel(id) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: {  token: token, id: id},
        url: 'index.php/api/newfile.Factory/factoryAdminDel'
    })
}

// 设备管理

// 获取设备列表
export function getDeviceList(params) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    const requestParams = {
        ...params,
        token: token
    };
    
    return request({
        method: 'GET',
        params: requestParams,
        url: 'index.php/api/newfile.Device/deviceList'
    })
}

// 新增设备
export function addDevice(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { ...data, token: token },
        url: 'index.php/api/newfile.Device/deviceAdd'
    })
}

// 编辑设备
export function editDevice(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { ...data, token: token },
        url: 'index.php/api/newfile.Device/deviceEdit'
    })
}

// 删除设备
export function deleteDevice(id) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    return request({
        method: 'POST',
        data: { id: id, token: token },
        url: 'index.php/api/newfile.Device/deviceDel'
    })
}

// 编辑客户信息
export function editCustomer(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    const cleanData = {};
    Object.keys(data).forEach(key => {
        if (data[key] !== null && data[key] !== undefined && data[key] !== '') {
            cleanData[key] = data[key];
        }
    });
    
    const requestData = {
        ...cleanData,
        token: token
    };
    
    return request({
        method: 'POST',
        data: requestData,
        url: 'index.php/api/newfile.Customer/customerEdit'
    })
}

// 合同管理

// 获取合同列表
export function getContractList(params) {
    const token = localStorage.getItem('token');
    const requestData = {
        ...params,
        token: token
    };
    
    return request({
        method: 'POST',
        data: requestData,
        url: 'index.php/api/newfile.Contract/contractList'
    })
}

// 添加合同
export function addContract(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    // 创建FormData对象用于文件上传
    const formData = new FormData();
    
    // 添加token
    formData.append('token', token);
    
    // 添加其他字段
    Object.keys(data).forEach(key => {
        if (data[key] !== null && data[key] !== undefined && data[key] !== '') {
            // 特殊处理图片路径数组字段
            if (key === 'img' && Array.isArray(data[key])) {
                // 处理img路径数组，将每个路径都添加到FormData
                data[key].forEach((url, index) => {
                    formData.append(`img[${index}]`, url);
                    console.log('添加图片路径到FormData:', `img[${index}]`, url);
                });
            } else if (key === 'img_payment' && Array.isArray(data[key])) {
                // 处理img_payment路径数组，将每个路径都添加到FormData
                data[key].forEach((url, index) => {
                    formData.append(`img_payment[${index}]`, url);
                    console.log('添加付款凭证图片路径到FormData:', `img_payment[${index}]`, url);
                });
            } else if ((key === 'img' || key === 'img_payment') && data[key] instanceof File) {
                // 处理单个文件（向后兼容）
                formData.append(key, data[key]);
                console.log('添加文件到FormData:', key, data[key]);
            } else {
                formData.append(key, data[key]);
            }
        }
    });

    return request({
        method: 'POST',
        data: formData,
        url: 'index.php/api/newfile.Contract/contractAdd3',
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

//获取合同编号
export function getContractNumber()
{
     const token = localStorage.getItem('token');
    
    return request({
        method: 'GET',
        params:{token},
        url: 'index.php/api/newfile.Contract/getCurrentContractNumber'
    })
}

// 编辑合同
export function editContract(data) {
    // 从localStorage获取token
    const token = localStorage.getItem('token');
    
    // 创建FormData对象用于文件上传
    const formData = new FormData();
    
    // 添加token
    formData.append('token', token);
    
    // 添加其他字段
    Object.keys(data).forEach(key => {
        if (data[key] !== null && data[key] !== undefined && data[key] !== '') {
            // 特殊处理图片路径数组字段
            if (key === 'img' && Array.isArray(data[key])) {
                // 处理img路径数组，将每个路径都添加到FormData
                data[key].forEach((url, index) => {
                    formData.append(`img[${index}]`, url);
                    console.log('添加图片路径到FormData:', `img[${index}]`, url);
                });
            } else if (key === 'img_payment' && Array.isArray(data[key])) {
                // 处理img_payment路径数组，将每个路径都添加到FormData
                data[key].forEach((url, index) => {
                    formData.append(`img_payment[${index}]`, url);
                    console.log('添加付款凭证图片路径到FormData:', `img_payment[${index}]`, url);
                });
            } else if ((key === 'img' || key === 'img_payment') && data[key] instanceof File) {
                // 处理单个文件（向后兼容）
                formData.append(key, data[key]);
                console.log('添加文件到FormData:', key, data[key]);
            } else {
                formData.append(key, data[key]);
            }
        }
    });

    return request({
        method: 'POST',
        data: formData,
        url: 'index.php/api/newfile.Contract/contractEdit2',
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

//删除合同deleteContract
export function deleteContract(id) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.Contract/contractDel',
    method: 'post',
    data :{
        id,
        token
    }
  });
}
// 配件管理相关API
export function getAccessoryList(params = {}) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.Device/accessoryList',
    method: 'get',
    params: {
      ...params,
      token
    }
  });
}

export function accessoryListTwo(params) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.Device/accessoryListTwo',
    method: 'get',
    params: {
      ...params,
      token
    }
  });
}

export function addAccessory(data) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.Device/accessoryAdd',
    method: 'post',
    data: {
      ...data,
      token
    }
  });
}

export function updateAccessory(data) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.Device/accessoryEdit',
    method: 'post',
    data :{
        ...data,
        token
    }
  });
}

export function deleteAccessory(id) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.Device/accessoryDel',
    method: 'post',
    data :{
        id,
        token
    }
  });
}

// 规格管理相关API

// 新增规格
export function addSku(data) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.AccessorySku/skuValueAdd',
    method: 'post',
    data: {
      ...data,
      token
    }
  });
}

// 编辑规格
export function updateSku(data) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.AccessorySku/skuValueEdit',
    method: 'post',
    data: {
      ...data,
      token
    }
  });
}

// 删除规格
export function deleteSku(id) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.AccessorySku/skuValueDel',
    method: 'post',
    data: {
      id,
      token
    }
  });
}

// 转单API
export function transferOrder(data) {
  const token = localStorage.getItem('token');
  return request({
    url: 'index.php/api/newfile.Orders/transferOrder',
    method: 'post',
    data: {
      ...data,
      token
    }
  });
}

